import requests
from common.b_yamlOperate import *
import re
getEnvC = YamlRead(pathConf.get_userFilePath("data/a_envData/env.yaml"))


def choiseHeadersHost(url,headers):
    """
    根据uri选择请求头里面的Host,修改headers里面的Host
    :param: headers 请求头
    """
    domainstr = re.findall(r'//[0-9A-Za-z\-.]+', url)[0]
    domain = domainstr[2:]
    headers['Host'] = domain
    return headers

class yu_Requests:

    def __init__(self):
        run_type = getEnvC.getdata()['run_type']
        if run_type == 0:
            getUserHeadC = YamlRead(pathConf.get_userFilePath("data/a_envData/testUser.yaml"))  # 获取用户头数据实例化类
            self.chb_user = getUserHeadC.getdata()
        elif run_type == 1:
            getUserHeadC = YamlRead(pathConf.get_userFilePath("data/a_envData/prodUser.yaml"))  # 获取用户头数据实例化类
            self.chb_user = getUserHeadC.getdata()

    def send_get(self,url,params=None,headers=None):
        if headers == None:
            res = requests.get(url=url, params=params)
        else:
            res = requests.get(url=url, params=params, headers=headers)
        return res

    def send_post(self,url,data=None,json=None,headers=None):
        if json == None:
            res=requests.post(url=url,data=data)
        else:
            res=requests.post(url=url,json=json,headers=headers)
        return res

    def send_put(self, url, data, json=None, headers=None):
        if headers == None:
            res = requests.put(url=url, data=data)
        else:
            res = requests.put(url=url, json=json, headers=headers)
        return res

    def send_delete(self, url, data, headers=None):
        if headers == None:
            res = requests.delete(url=url, data=data)
        else:
            res = requests.delete(url=url, data=data, headers=headers)
        return res

    def send_options(self):
        pass

    def choise_user(self,user_id):
        """
        传入 user_id ，取用户请求头
        :param: user_id 输入类型 数字,  或者传入真实的 字符串类型的 headers
        """

        user_id_key = "user_" + str(user_id)
        if user_id == 9475830:
            # print("输入正确")
            headers = self.chb_user[user_id_key]
        elif user_id == 662068:
            headers = self.chb_user[user_id_key]
        elif user_id == 662066:
            headers = self.chb_user[user_id_key]
        elif user_id == 662085:
            headers = self.chb_user[user_id_key]
        elif user_id == 662095:
            headers = self.chb_user[user_id_key]
        elif user_id in self.chb_user['users']:
            headers = self.chb_user[user_id_key]
        else:
            headers = eval(user_id)
        return headers

    def send_request(self, method, url, data=None,json=None, headers=None):
        res=None
        # 适应多环境

        if method=='GET':
            headers = self.choise_user(headers)
            # print("GET请求开始")
            res = self.send_get(url=url,params=data, headers=headers)
            # print("GET请求完成")
        elif method=='POST':
            headers = self.choise_user(headers)
            headers = choiseHeadersHost(url=url,headers=headers)
            # print(headers)
            res = self.send_post(url=url,data=data, json=json,headers=headers)
            # print("POST请求完成")
        elif method=='PUT':
            # print("PUT请求开始")
            headers = self.choise_user(headers)
            res = self.send_put(url=url,data=data, headers=headers)
            # print("PUT请求完成")
        elif method=='DELETE':
            headers = self.choise_user(headers)
            res = self.send_delete(url=url,data=data, headers=headers)
        return res


    def ex_request(self,data, user_id):
        """ 执行请求
        :param: user_id 输入类型 数字; 非数字就必须是请求头字符串
        :param: datd 字典参数
        :return: test 返回响应字符串
        """
        method = data['request_method']
        print("method:",method)
        url = data['domain'] + data['api_uri']
        print("url:",url)
        header = data['request_headers']
        json = data['request_body']
        print("request_body:",json)
        headers = header

        req = yu_Requests()
        res = req.send_request(
            method=method,
            url=url,
            json=json,
            headers=user_id
        )
        # 防止出现响应文本为乱码，方法1
        res.encoding = 'utf-8'
        # 防止出现响应文本为乱码，方法2
        # res.encoding("latin-1")
        # res.decode("utf-8")
        text = res.text
        self.response = text
        return text
        # print(text)

yu_requestsC = yu_Requests()

if __name__ == "__main__":
    pass
    yu_requestsC = yu_Requests()

